package com.ruoyi.admin.service.faq.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.admin.domain.LlmFaqDownvoteDetail;
import com.ruoyi.admin.mapper.LlmFaqDownvoteDetailMapper;
import com.ruoyi.admin.service.faq.LlmFaqDownvoteDetailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class LlmFaqDownvoteDetailServiceImpl implements LlmFaqDownvoteDetailService {

    private final LlmFaqDownvoteDetailMapper llmFaqDownvoteDetailMapper;

    @Override
    public boolean addFaqDetail(LlmFaqDownvoteDetail llmFaqDownvoteDetail) {
        llmFaqDownvoteDetail.setDownvoteTime(LocalDateTime.now());

        return insertDownvote(llmFaqDownvoteDetail);
    }

    public boolean insertDownvote(LlmFaqDownvoteDetail llmFaqDownvoteDetail) {
        return llmFaqDownvoteDetailMapper.insert(llmFaqDownvoteDetail) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFaqDetail(String faqDownvoteId) {
        int i = llmFaqDownvoteDetailMapper.deleteById(faqDownvoteId);
        return i > 0;
    }

    @Override
    public boolean editFaqDetail(LlmFaqDownvoteDetail llmFaqDownvoteDetail) {
        QueryWrapper<LlmFaqDownvoteDetail> llmFaqDownvoteQueryWrapper = new QueryWrapper<LlmFaqDownvoteDetail>();
        llmFaqDownvoteQueryWrapper.eq("id", llmFaqDownvoteDetail.getId());
        LlmFaqDownvoteDetail llmFaqDownvote = llmFaqDownvoteDetailMapper.selectOne(llmFaqDownvoteQueryWrapper);

        if (llmFaqDownvote == null) {
            return false;
        }

        UpdateWrapper<LlmFaqDownvoteDetail> llmFaqDownvoteUpdateWrapper = new UpdateWrapper<>();
        llmFaqDownvoteUpdateWrapper.eq("id", llmFaqDownvote.getId());
        llmFaqDownvoteUpdateWrapper.set("faq_id", llmFaqDownvote.getFaqId());
        llmFaqDownvoteUpdateWrapper.set("user_id", llmFaqDownvote.getUserId());
        llmFaqDownvoteUpdateWrapper.set("conversation_id", llmFaqDownvote.getConversationId());
        llmFaqDownvoteUpdateWrapper.set("msg_id", llmFaqDownvote.getMsgId());
        llmFaqDownvoteUpdateWrapper.set("problem", llmFaqDownvote.getProblem());
        llmFaqDownvoteUpdateWrapper.set("answer", llmFaqDownvote.getAnswer());
        llmFaqDownvoteUpdateWrapper.set("downvote_time", llmFaqDownvote.getDownvoteTime());
        llmFaqDownvoteUpdateWrapper.set("reason", llmFaqDownvote.getReason());
        llmFaqDownvoteUpdateWrapper.set("remark", llmFaqDownvote.getRemark());
        llmFaqDownvoteUpdateWrapper.set("type", llmFaqDownvote.getType());
        llmFaqDownvoteUpdateWrapper.set("user_name", llmFaqDownvote.getUserName());

        return llmFaqDownvoteDetailMapper.update(llmFaqDownvoteUpdateWrapper) != 0;
    }

    @Override
    public LlmFaqDownvoteDetail getFaqDetailById(String id) {
        QueryWrapper<LlmFaqDownvoteDetail> llmFaqDownvoteQueryWrapper = new QueryWrapper<>();
        llmFaqDownvoteQueryWrapper.eq("id", id);
        return llmFaqDownvoteDetailMapper.selectOne(llmFaqDownvoteQueryWrapper);
    }

    @Override
    public List<LlmFaqDownvoteDetail> list() {
        QueryWrapper<LlmFaqDownvoteDetail> llmFaqDownvoteQueryWrapper = new QueryWrapper<>();
        return llmFaqDownvoteDetailMapper.selectList(llmFaqDownvoteQueryWrapper);
    }

    @Override
    public List<LlmFaqDownvoteDetail> getFaqDownvoteDtailByConversationId(String conversationId, String userId) {

        QueryWrapper<LlmFaqDownvoteDetail> llmFaqDownvoteDetailQueryWrapper = new QueryWrapper<>();
        llmFaqDownvoteDetailQueryWrapper.eq("conversation_id", conversationId)
                .eq("user_id", userId);
        return llmFaqDownvoteDetailMapper.selectList(llmFaqDownvoteDetailQueryWrapper);
    }
}
